package com.example.xianji.activity;


import android.net.Uri;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

public class Main_SheZhiActivity extends Activity implements OnClickListener {
    private RelativeLayout shezhi_back;
    private RelativeLayout shezhi_relativelayout_help, shezhi_relativelayout_guanyu, shezhi_relativelayout_yijian, shezhi_call_phone, shezhi_geren_zhongxin, shezhi_relative_delete;
    private Dialog dialog;
    private Button button_exit;
//    private SharedPreferences sp;
//    private Editor ed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__she_zhi);
        //XJApplication.getInstance().addActivity(this);

        //XJApplication.getInstance4().addActivity4(this);
        shezhi_back = (RelativeLayout) findViewById(R.id.shezhi_relative_back);
        shezhi_relativelayout_help = (RelativeLayout) findViewById(R.id.shezhi_relativelayout_help);
        shezhi_relativelayout_guanyu = (RelativeLayout) findViewById(R.id.shezhi_relativelayout_guanyu);
        shezhi_relativelayout_yijian = (RelativeLayout) findViewById(R.id.shezhi_relativelayout_yijian);
        shezhi_call_phone = (RelativeLayout) findViewById(R.id.shezhi_call_phone);
        shezhi_geren_zhongxin = (RelativeLayout) findViewById(R.id.shezhi_geren_zhongxin);
        button_exit = (Button) findViewById(R.id.button_exit);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();

        shezhi_relative_delete = (RelativeLayout) findViewById(R.id.shezhi_relative_delete);

        shezhi_back.setOnClickListener(this);
        shezhi_relativelayout_help.setOnClickListener(this);
        shezhi_relativelayout_guanyu.setOnClickListener(this);
        shezhi_relativelayout_yijian.setOnClickListener(this);
        shezhi_call_phone.setOnClickListener(this);
        shezhi_geren_zhongxin.setOnClickListener(this);
        button_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.shezhi_relative_back) {
            this.finish();
        } else if (v.getId() == R.id.shezhi_relativelayout_help) {
            startActivity(new Intent(Main_SheZhiActivity.this, Main_HelpActivity.class));
        } else if (v.getId() == R.id.shezhi_geren_zhongxin) {
            Intent intent = new Intent(Main_SheZhiActivity.this, Main_GeRen_ZhongXinActivity.class);
            startActivityForResult(intent, Contant.requestCode);
        } else if (v.getId() == R.id.shezhi_relativelayout_guanyu) {
            startActivity(new Intent(Main_SheZhiActivity.this, Main_GuanYu_XianJiActivity.class));
        } else if (v.getId() == R.id.shezhi_relativelayout_yijian) {
            startActivity(new Intent(Main_SheZhiActivity.this, Main_YiJian_FanKuiActivity.class));
        } else if (v.getId() == R.id.shezhi_call_phone) {
            dialog = new AlertDialog.Builder(Main_SheZhiActivity.this).create();
            dialog.show();
            Window window = dialog.getWindow();
            window.setContentView(R.layout.view_child);
            ImageView shanchu = (ImageView) window.findViewById(R.id.shanchu);
            ImageView dianhua = (ImageView) window.findViewById(R.id.dianhua);
            TextView tel = (TextView) window.findViewById(R.id.tel);
            shanchu.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    dialog.dismiss();
                }
            });
            dianhua.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + "400-615-8109"));
                    startActivity(intent);
                }
            });
        } else if (v.getId() == R.id.button_exit) {
            PreferenceEngine.getInstance().putInt(Constant.SP.NAME, Constant.SP.KEY_MY_XIANJI_SET, 2);
            PreferenceEngine.getInstance().putInt(Constant.SP.NAME, Constant.SP.KEY_FINISH_FROM, 2);
            PreferenceEngine.getInstance().putBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false);
            Contant.login_resume = 1;
            System.out.print(Contant.login_resume + "");
            Main_SheZhiActivity.this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Contant.requestCode && resultCode == Contant.back_resultCode) {
            shezhi_relative_delete.setVisibility(View.VISIBLE);
            shezhi_relative_delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Main_SheZhiActivity.this.finish();
                }
            });
        } else if (requestCode == Contant.requestCode && resultCode == Contant.delete_resultCode) {
            Main_SheZhiActivity.this.finish();
        }
    }
}
